package com.itheima.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
@RequestMapping("/uploadController")
public class UploadController {

    @RequestMapping("/upload")
    @ResponseBody
    public void upload(MultipartFile[] files, HttpServletRequest request) throws IOException {
        for (MultipartFile multipartFile : files) {
            if (!multipartFile.isEmpty()){
                String realPath = request.getServletContext().getRealPath("/upload");
                String filename = multipartFile.getOriginalFilename();
                String uuid = UUID.randomUUID().toString().replace("-", "");
                String suffix = filename.substring(filename.lastIndexOf("."));
                filename=uuid+suffix;
                multipartFile.transferTo(new File(realPath,filename));

            }
        }
    }
}
